home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / AE Stuff / AEUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  4.8 KB  |  186 lines  |  [TEXT/KAHL]

  1. /*
  2.  * ---------------------------------------------------------------------
  3.  * AEUtilities.c :
  4.  *
  5.  * © Copyright 1992 by F. Menneteau.  All rights reserved.
  6.  * This code can be both distributed and used freely.
  7.  *
  8.  * ---------------------------------------------------------------------
  9.  */
  10.  
  11.  
  12. #include "CGenericAE.h"
  13.  
  14.  
  15.     /* ---------------- */
  16.     /*     GetTargetID (G)    */
  17.     /* ---------------- */
  18. OSErr GetTargetID(
  19.     Str31        TheZone,
  20.     Str31        TheServer,        // The (remote or local) machine
  21.     Str32        TheTargetName,    
  22.     TargetIDPtr    TheTargetIDPtr
  23. )
  24. {
  25.     OSErr                LcOSErr                ;
  26.     IPCListPortsPBRec    LcIPCListPortsPBRec    ;
  27.     PortInfoRec            LcBufferPortInfoRec    ;    // Usually an array
  28.     LocationNameRec        LcLocNameRec        ;
  29.     PPCPortRec            LcPPCPortRec        ;
  30.  
  31.     /*
  32.      *    Notes:
  33.      *    TheZone can be obtain either by calling GetZoneList() (which
  34.      *    gives a list of all the zone names on the internet), or
  35.      *    GetLocalZones() (list of zone names on the local network), or
  36.      *    GetMyZone() (the AppleTalk zone name of your local node).
  37.      *
  38.      *    TheServer can be obtain by calling PLookupName() (which gives
  39.      *    a list of computers with ports).
  40.      */
  41.  
  42.     /*
  43.      *    Build the PPCPortRec structure
  44.      */
  45.     LcPPCPortRec.nameScript    = smRoman                        ;
  46.     pstrcpy((Ptr) LcPPCPortRec.name,(Ptr) TheTargetName)    ;
  47.  
  48.     LcPPCPortRec.portKindSelector = ppcByString                ;
  49.     pstrcpy((Ptr) LcPPCPortRec.u.portTypeStr,(Ptr) "\p=")    ;    // Match all types
  50.     
  51.     /*
  52.      *    Build the LocationNameRec structure (ppcNoLocation is used
  53.      *    when the location is on the local machine).
  54.      */
  55.     LcLocNameRec.locationKindSelector = ppcNBPLocation                ;
  56.     pstrcpy((Ptr) LcLocNameRec.u.nbpEntity.objStr,(Ptr) TheServer)    ;
  57.     pstrcpy((Ptr) LcLocNameRec.u.nbpEntity.typeStr,PPCTOOLBOX)        ;
  58.     pstrcpy((Ptr) LcLocNameRec.u.nbpEntity.zoneStr,(Ptr) TheZone)    ;
  59.  
  60.     /*
  61.      *    Build the LcIPCListPortsPBRec structure.
  62.      */
  63.     LcIPCListPortsPBRec.startIndex     = 0                    ;
  64.     LcIPCListPortsPBRec.requestCount = 1                    ;
  65.     LcIPCListPortsPBRec.portName     = &LcPPCPortRec        ;
  66.     LcIPCListPortsPBRec.locationName = &LcLocNameRec        ;
  67.     LcIPCListPortsPBRec.bufferPtr     = &LcBufferPortInfoRec    ;    // 1 item
  68.  
  69.     /*
  70.      *    This function returns information about ports that are on the
  71.      *    computer (TheServer) specified in the locationName field of
  72.      *    the list ports parameter block: IPCListPortsPBRec.locationName.
  73.      *
  74.      *    Here however, we only search for the port of the TheTargetName.
  75.      */
  76.     LcOSErr = IPCListPorts(&LcIPCListPortsPBRec,FALSE) ;
  77.  
  78.     if ( (LcIPCListPortsPBRec.actualCount == 1 ) && (LcOSErr == noErr) ) {
  79.         TheTargetIDPtr->name    = LcBufferPortInfoRec.name    ;
  80.         TheTargetIDPtr->location= LcLocNameRec                ;
  81.     }
  82.  
  83.     return(LcOSErr) ;
  84. }    
  85.  
  86.  
  87.     /* ------------------------ */
  88.     /*     EntityPathToAlias (G)    */
  89.     /* ------------------------ */
  90. OSErr EntityPathToAlias(
  91.     Ptr            TheEntityPath,
  92.     TypeEntity    TheTypeEntity,
  93.     AliasHandle    *TheAliasHandle)
  94. {
  95.     Str255        LcEntityPath    ;
  96.     OSErr        LcOSErr            ;
  97.     FSSpec        LcFSSpec        ;
  98.  
  99.     if ( TheEntityPath == (Ptr) NULL )
  100.         return(bdNamErr) ;
  101.  
  102.     /*
  103.      *    Save Entity name because we can modify it.
  104.      */
  105.     pstrcpy((Ptr) LcEntityPath,TheEntityPath) ;
  106.  
  107.     if ( (TheTypeEntity == typeFOLDER) &&
  108.                 PSTRLASTCHAR(LcEntityPath) != PSTRLASTCHAR(PDELIMITER)) {
  109.         pstrcat((Ptr) LcEntityPath,PDELIMITER) ;
  110.     }
  111.  
  112.     LcOSErr = FSMakeFSSpec(0,0,LcEntityPath,&LcFSSpec)    ;
  113.     RETN_IF_ERR(LcOSErr)                                ;
  114.  
  115.     LcOSErr = NewAlias((FSSpecPtr) NULL,&LcFSSpec,TheAliasHandle)    ;
  116.     RETN_IF_ERR(LcOSErr)                                            ;
  117.  
  118.     if ( *TheAliasHandle == (AliasHandle) NULL )
  119.         return(nilHandleErr) ;
  120.  
  121.     return(noErr) ;
  122. }
  123.  
  124.  
  125.     /* -------------------- */
  126.     /*     GetProcessPSN (G)    */
  127.     /* -------------------- */
  128. OSErr GetProcessPSN(
  129.     OSType                    TheProcessType,
  130.     OSType                    TheProcessSignature,
  131.     ProcessSerialNumberPtr    ThePSNPtr)
  132. {
  133.     ProcessInfoRec    LcProcessInfo    ;
  134.     FSSpec            LcFSSpec        ;
  135.  
  136.     LcProcessInfo.processName        = (StringPtr) NULL        ;
  137.     LcProcessInfo.processInfoLength = sizeof(LcProcessInfo)    ;
  138.     LcProcessInfo.processAppSpec    = &LcFSSpec                ;
  139.     
  140.     ThePSNPtr->highLongOfPSN = 0            ;
  141.     ThePSNPtr->lowLongOfPSN  = kNoProcess    ;
  142.     
  143.     while( !GetNextProcess(ThePSNPtr) ) {
  144.         if( !GetProcessInformation(ThePSNPtr,&LcProcessInfo) &&
  145.             (LcProcessInfo.processType == TheProcessType)  &&
  146.             (LcProcessInfo.processSignature  == TheProcessSignature) ) {
  147.                 return(noErr) ;
  148.         }
  149.     }
  150.  
  151.     return(procNotFound) ;
  152. }
  153.  
  154.  
  155.     /* ---------------- */
  156.     /*     KillProcess (G)    */
  157.     /* ---------------- */
  158. OSErr KillProcess(
  159.     Boolean    TheAskApplFlag,
  160.     Boolean    TheWaitReplyFlag,
  161.     OSType    TheProcessType,
  162.     OSType    TheProcessSignature)
  163. {
  164.     OSErr        LcOSErr                        ;
  165.     CGenericAE    *LcCAE = new(CGenericAE)    ;
  166.  
  167.     if ( TheAskApplFlag )
  168.         LcOSErr = LcCAE->AskAndBuildTarget(sPSN) ;
  169.     else
  170.         LcOSErr = LcCAE->BuildTargetWithTypeAndSign(
  171.                                     TheProcessType,TheProcessSignature)    ;
  172.     RETN_IF_ERR(LcOSErr)    ;
  173.  
  174.     LcOSErr = LcCAE->CreateAE(kCoreEventClass,kAEQuitApplication)    ;
  175.     RETN_IF_ERR(LcOSErr)                                            ;
  176.  
  177.     if ( TheWaitReplyFlag )
  178.         LcOSErr = LcCAE->SendAEWithReply() ;
  179.     else
  180.         LcOSErr = LcCAE->SendAEWithoutReply() ;
  181.  
  182.     delete(LcCAE)    ;
  183.     return(LcOSErr) ;
  184. }
  185.  
  186.